<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class obat_data extends CI_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('m_obat_data');
		$this->load->library('breadcrumb');	
	}

	public function index(){
		$this->rule->type('R');
		$this->load->library('paging');

		//Menghapus session pencarian
		$this->session->set_userdata('keyword_obat');
		$this->session->set_userdata('filter');

		$config['sql'] = $this->m_obat_data->get_all();
		$config['per_page'] = 6;
		$data = $this->paging->create_pagination($config);
		$judul = "Data Obat";
		$data['judul'] = $judul;
		$this->layout->set_title($judul);
		$this->layout->set_desc("lorem ipsum");

		#breadcrumbs
		$this->breadcrumb->clear();
		$this->breadcrumb->add_crumb('Dasbor', site_url('dasbor'));
		$this->breadcrumb->add_crumb('Transaksi', site_url('obat_kelompok#'));
		$this->breadcrumb->add_crumb($judul);

		$this->layout->set_include('css', 'themes/apotek/css/olide-custom.css');
		$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/jquery.validate.min.js', 'header');
		$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/jquery.validate.min.js', 'header');
		$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/localization/messages_id.min.js', 'header');
		$this->layout->set_include('js', 'themes/general/bundle/select2/select2.min.js', 'header');
		$this->layout->set_include('css', 'themes/general/bundle/select2/select2.min.css');


		//untuk motong kalimat
		$this->load->helper('trim_text');

		$this->layout->theme('apotek', 'obat_data/index', $data);		
	}

	public function search(){
		$this->rule->type('R');

		if(isset($_POST)){
			$input = $this->input->post();
			$data['keyword'] = $this->input->post('keyword');
			$data['filter'] = $this->input->post('filter');
			$this->session->set_userdata('keyword_obat', $data['keyword']);
			$this->session->set_userdata('filter', $data['filter']);

			#pagination
			$this->load->library('paging');
			$config['sql'] = $this->m_obat_data->search($data);
			$config['per_page'] = 5;
			$data = $this->paging->create_pagination($config);

			$this->layout->set_include('css', 'themes/apotek/css/olide-custom.css');

			$judul = 'Pencarian obat "' . $this->session->userdata('keyword_obat') . '"';
			$data['judul']  = 'Pencarian obat " <font color="#FC771F">' . $this->session->userdata('keyword_obat') . '</font> "';

			$this->layout->set_title($judul);
			$this->layout->set_desc('Made with love by Raksa Abadi Informatika');

    	#import css + js
			$this->layout->set_include('css', 'themes/apotek/css/path-to-file.css');
			$this->layout->set_include('js', 'themes/apotek/css/path-to-file.js');
			$this->layout->set_include('js', 'themes/apotek/css/path-to-file.js', 'header');

			#breadcrumbs
			$this->breadcrumb->clear();
			$this->breadcrumb->add_crumb('Dasbor', site_url('dasbor'));
			$this->breadcrumb->add_crumb('Obat', site_url('obat_kelompok#'));
			$this->breadcrumb->add_crumb('Kelompok Obat', site_url('obat_kelompok'));
			$this->breadcrumb->add_crumb($judul);

			$this->load->helper('trim_text');

			$this->layout->theme('apotek','obat_data/index', $data);
		} else {
			redirect('obat', 'refresh');
		}
	}

	public function add(){
		$this->rule->type('C');

		if (isset($_POST) && !empty($_POST)) {
			$input  = $this->input->post();
			$this->m_obat_data->store($input);

			redirect('obat_data', 'refresh');
		}else{
			$judul = 'Tambah Data Obat';
			$data['judul']  = $judul;

			$this->layout->set_title($judul);
			$this->layout->set_desc('Made with love by Raksa Abadi Informatika');

			$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/jquery.validate.min.js', 'header');
			$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/jquery.validate.min.js', 'header');
			$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/localization/messages_id.min.js', 'header');
			$this->layout->set_include('js', 'themes/general/bundle/select2/select2.min.js', 'header');
			$this->layout->set_include('css', 'themes/general/bundle/select2/select2.min.css');

	    #breadcrumbs
			$this->breadcrumb->clear();
			$this->breadcrumb->add_crumb('Dasbor', site_url('dasbor'));
			$this->breadcrumb->add_crumb('Obat', site_url('obat_data#'));
			$this->breadcrumb->add_crumb('Data Obat', site_url('obat_data'));
			$this->breadcrumb->add_crumb($judul);

			$this->layout->theme('apotek','obat_data/add', $data);
		}
	}

	public function edit($id){
		$this->rule->type('U');

		if (isset($_POST) && !empty($_POST)) {
			$input  = $this->input->post();
			$this->m_obat_data->update($input);

			redirect('obat_data', 'refresh');
		}else{
			$this->load->helper('cek_id');
			if(!cek_id($id, 'KdObat', 'obat')){
				show_404();
			}

			$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/jquery.validate.min.js', 'header');
			$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/localization/messages_id.min.js', 'header');
			$this->layout->set_include('js', 'themes/general/bundle/select2/select2.min.js', 'header');
			$this->layout->set_include('css', 'themes/general/bundle/select2/select2.min.css');

			$judul = 'Edit Data Obat';
			$data['judul'] = $judul;

			$this->layout->set_title($judul);
			$this->layout->set_desc('Made with love by Raksa Abadi Informatika');

  		#breadcrumbs
			$this->breadcrumb->clear();
			$this->breadcrumb->add_crumb('Dasbor', site_url('dasbor'));
			$this->breadcrumb->add_crumb('Obat', site_url('obat_kelompok#'));
			$this->breadcrumb->add_crumb('Data Obat', site_url('obat_data'));
			$this->breadcrumb->add_crumb($judul);

			$data['data'] = $this->m_obat_data->get_by_id($id);

			$this->layout->theme('apotek','obat_data/edit', $data);
		}
	}

	public function delete($id){
		$this->rule->type('D');

		$this->load->helper('cek_id');
		if(!cek_id($id, 'KdObat', 'obat')){
			set_flash('danger','Ups, Data obat tidak ditemukan');
			redirect('obat_data','refresh');
		}

		$this->m_obat_data->destroy($id);

		redirect($_SERVER['HTTP_REFERER'],'refresh');
	}

	public function excel($filter="", $keyword=""){
		$this->rule->type('R');
		
		#load our new PHPExcel library
		$this->load->library('excel');
		#activate worksheet number 1
		$this->excel->setActiveSheetIndex(0);
		#name the worksheet
		$this->excel->getActiveSheet()->setTitle('Data Obat');
		#set cell A1 content with some text
		if($filter != "" && $keyword != "" && $keyword != " "){
			if($filter == "by_name" || $filter == "by_info"){
				$this->excel->getActiveSheet()->setCellValue('A1', 'Obat Berdasarkan Kata Kunci "'.$keyword.'" '.site_title());
			}else if($filter == "by_code"){
				$this->excel->getActiveSheet()->setCellValue('A1', 'Obat Berdasarkan Kode "'.$keyword.'" '.site_title());
			}
		}else{
			$this->excel->getActiveSheet()->setCellValue('A1', 'Obat '.site_title());
		}

		$this->excel->getActiveSheet()->setCellValue('A2', 'No.');
		$this->excel->getActiveSheet()->setCellValue('B2', 'Kode Obat');
		$this->excel->getActiveSheet()->setCellValue('C2', 'Nama Obat');
		$this->excel->getActiveSheet()->setCellValue('D2', 'Kelompok');
		$this->excel->getActiveSheet()->setCellValue('E2', 'Min. Dosis');
		$this->excel->getActiveSheet()->setCellValue('F2', 'Max. Dosis');
		$this->excel->getActiveSheet()->setCellValue('G2', 'Satuan');
		$this->excel->getActiveSheet()->setCellValue('H2', 'Satuan Dosis');
		$this->excel->getActiveSheet()->setCellValue('I2', 'Informasi');
		#change the font size
		$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setSize(11);
		#make the font become bold
		$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
		#merge cell A1 until D1
		$this->excel->getActiveSheet()->mergeCells('A1:I1');
		#set aligment to center for that merged cell (A1 to D1)
		$this->excel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		#set style
		$TitleStyle = array(
			'borders' => array(
				'allborders' => array(
					'style' => PHPExcel_Style_Border::BORDER_THIN
					)
				),
			'fill' => array(
				'type' => PHPExcel_Style_Fill::FILL_SOLID,
            //'color' => array('rgb' => 'FFFFFF') // hijau tua
				),
			'font'  => array(
				'bold'  => true,
  			//'color' => array('rgb' => '#666666'),
				'size'  => 12,
				'name'  => 'calibri'
				)
			);

		$HeaderStyle = array(
			'borders' => array(
				'allborders' => array(
					'style' => PHPExcel_Style_Border::BORDER_THIN
					)
				),
			'fill' => array(
				'type' => PHPExcel_Style_Fill::FILL_SOLID,
            //'color' => array('rgb' => 'FFFFFF') // hijau
				),
			'font'  => array(
				'bold'  => true,
				'size'  => 12,
				'name'  => 'calibri'
				)
			);

		$ContentStyle = array(
			'borders' => array(
				'allborders' => array(
					'style' => PHPExcel_Style_Border::BORDER_THIN
					)
				),
			'fill' => array(
				'type' => PHPExcel_Style_Fill::FILL_SOLID,
            //'color' => array('rgb' => 'FFFFFF') // kuning
				),
			'font'  => array(
				'size'  => 12,
				'name'  => 'calibri'
				)
			);

		$this->excel->getActiveSheet()->getStyle('A1:I1')->applyFromArray($TitleStyle);
		$this->excel->getActiveSheet()->getStyle('A2:I2')->applyFromArray($HeaderStyle);
		//isi
		if($filter != "" && $keyword != "" && $keyword != " "){
			$list_data = $this->m_obat_data->export_by($filter, $keyword);
		}else{
			$list_data = $this->m_obat_data->export();
		}

		$cols = 3;
		$num = 1;
		foreach($list_data as $row){
			$col = $cols++;
			$this->excel->getActiveSheet()->setCellValue('A' . $col, $num++);
			$this->excel->getActiveSheet()->setCellValue('B' . $col, $row['KdObat']);
			$this->excel->getActiveSheet()->setCellValue('C' . $col, $row['NmObat']);
			$this->excel->getActiveSheet()->setCellValue('D' . $col, $row['NmKelompok']);
			$this->excel->getActiveSheet()->setCellValue('E' . $col, $row['MinDosis']);
			$this->excel->getActiveSheet()->setCellValue('F' . $col, $row['MaxDosis']);
			$this->excel->getActiveSheet()->setCellValue('G' . $col, $row['Satuan']);
			$this->excel->getActiveSheet()->setCellValue('H' . $col, $row['SatuanDosis']);
			$this->excel->getActiveSheet()->setCellValue('I' . $col, $row['Informasi']);
		}

		#warna
		$cols2 = 3;
		foreach($list_data as $row){
			$col2 = $cols2++;
			$this->excel->getActiveSheet()->getStyle('A' . $col2)->applyFromArray($ContentStyle);
			$this->excel->getActiveSheet()->getStyle('B' . $col2)->applyFromArray($ContentStyle);
			$this->excel->getActiveSheet()->getStyle('C' . $col2)->applyFromArray($ContentStyle);
			$this->excel->getActiveSheet()->getStyle('D' . $col2)->applyFromArray($ContentStyle);
			$this->excel->getActiveSheet()->getStyle('E' . $col2)->applyFromArray($ContentStyle);
			$this->excel->getActiveSheet()->getStyle('F' . $col2)->applyFromArray($ContentStyle);
			$this->excel->getActiveSheet()->getStyle('G' . $col2)->applyFromArray($ContentStyle);
			$this->excel->getActiveSheet()->getStyle('H' . $col2)->applyFromArray($ContentStyle);
			$this->excel->getActiveSheet()->getStyle('I' . $col2)->applyFromArray($ContentStyle);
		}


		$this->excel->getActiveSheet()->getColumnDimension("A")->setAutoSize(true);
		$this->excel->getActiveSheet()->getColumnDimension("B")->setAutoSize(true);
		$this->excel->getActiveSheet()->getColumnDimension("C")->setAutoSize(true);
		$this->excel->getActiveSheet()->getColumnDimension("D")->setAutoSize(true);
		$this->excel->getActiveSheet()->getColumnDimension("E")->setAutoSize(true);
		$this->excel->getActiveSheet()->getColumnDimension("F")->setAutoSize(true);
		$this->excel->getActiveSheet()->getColumnDimension("G")->setAutoSize(true);
		$this->excel->getActiveSheet()->getColumnDimension("H")->setAutoSize(true);
		$this->excel->getActiveSheet()->getColumnDimension("I")->setAutoSize(true);


		$filename='data kelompok obat.xls'; //save our workbook as this file name
		header('Content-Type: application/vnd.ms-excel'); //mime type
		header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
		header('Cache-Control: max-age=0'); //no cache

		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		//if you want to save it as .XLSX Excel 2007 format
		$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');  
		//force user to download the Excel file without writing it to server's HD
		$objWriter->save('php://output');
	}
}

/* End of file obat_data.php */
/* Location: ./application/controllers/obat_data.php */